<template>
  <div>
    <div class="box"><h2 v-text="carouselIndex.carouselName">轮播名称</h2></div>
    <div class="box">
      <el-button type="primary" @click="handleAddImage">增加图片</el-button>
    </div>
    <div class="box">
      <el-table :data="tableData" border style="width: 100%">
        <el-table-column prop="bodyId" label="ID" width="80">
        </el-table-column>
        <el-table-column prop="title" label="图片名称" width="320">
        </el-table-column>
        <el-table-column prop="webHref" label="轮播链接" width="320">
        </el-table-column>
        <el-table-column label="图片" width="200">
          <template slot-scope="scope">
            <el-image style="width: 100px; height: 100px" :src="scope.row.imageUrl" fit="contain"></el-image>
          </template>
        </el-table-column>
        <el-table-column prop="sortRank" label="显示顺序" width="80">
        </el-table-column>
        <el-table-column prop="updateTime" label="更新时间">
        </el-table-column>
        <el-table-column label="操作" width="280">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 轮播对话框 -->
    <el-dialog :title="formTitle" :visible.sync="dialogFormVisible">
      <el-form ref="form" :model="form" :rules="rules">
        <el-form-item label="图片名称" :label-width="formLabelWidth" prop="title">
          <el-input v-model="form.title" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="轮播图片" :label-width="formLabelWidth" prop="imageUrl">
          <div>
            <img v-if="form.imageUrl" :src="form.imageUrl" class="avatar" />
          </div>
          <div>
            <el-button type="primary" icon="el-icon-upload2" style="margin-top: 6px;" @click="handleShowImageForm">选择图片</el-button>
          </div>
        </el-form-item>
        <el-form-item label="链接地址" :label-width="formLabelWidth" prop="webHref">
          <el-input v-model="form.webHref" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="显示顺序" :label-width="formLabelWidth" prop="sortRank">
          <el-input v-model="form.sortRank" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitCarousel">保 存</el-button>
      </div>
    </el-dialog>

    <!-- 选择图片 -->
    <el-dialog title="选择图片" :visible.sync="imgFormVisible">
      <div>
        <div>
          <!-- <vue-select-image :data-images="dataImages" :h="'100'" :w="'140'" :use-label="true" @onselectimage="onSelectImage"> -->
          <vue-select-image :data-images="dataImages" :h="'100'" :w="'100'" @onselectimage="onSelectImage">
          </vue-select-image>
        </div>
        <div>
          <el-pagination
            background
            layout="total, prev, pager, next"
            :current-page="pageNum"
            :page-size="pageSize"
            :total="total"
            @current-change="handleCurrentChange"
            @size-change="handleSizeChange"
            >
          </el-pagination>
        </div>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="imgFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitSelectImage">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
  import {
    bannerAddIndex,
    bannerSetIndex,
    bannerDelIndex,
    bannerGetIndex,
    bannerListIndex,
    bannerAddBody,
    bannerSetBody,
    bannerDelBody,
    bannerGetBody,
    bannerListBody,
    getResourceList
  } from '@/api/news.js'
  import VueSelectImage from 'vue-select-image'
  require('vue-select-image/dist/vue-select-image.css')

  export default {
    name: 'AddCarousel',
    components: { VueSelectImage },
    props: {
      carouselId: { type: [String,Number], default: 0 },
    },
    data() {
      return {
        carouselIndex: {
          carouselName: '',
        },
        tableData: [],
        total: 0,
        pageSize: 10,
        pageNum: 1,
        formTitle: '增加轮播图片',
        dialogFormVisible: false,
        formLabelWidth: '120px',
        form: {
          bodyId: 0,
          carouselId: 0,
          title: '',
          webHref: '',
          imageUrl: '',
          sortRank: 99999,
        },
        dataImages: [],
        rules: {
          title: [
            { required: true, message: '请输入图片名称', trigger: 'change' }
          ],
          imageUrl: [
            { required: true, message: '请选择图片', trigger: 'change' }
          ],
        },

        imgFormVisible: false,

      }
    },
    mounted() {
      this.loadCarouselById()
      this.loadList()
    },
    methods: {
      resetForm() {
        this.form.bodyId = 0
        this.form.carouselId = this.carouselId
        this.form.title = ''
        this.form.webHref = ''
        this.form.imageUrl = ''
        this.form.sortRank = 99999
      },
      loadCarouselById() {
        let param = {id: this.carouselId}
        bannerGetIndex(param).then(res => {
          if(res.code == 200) {
            this.carouselIndex = res.data
          }
        })

      },
      loadList() {
        let param = {
          pageNum: this.pageNum,
          pageSize: this.pageSize,
          id: this.carouselId
        }
        bannerListBody(param).then(res => {
          console.log('getCarouselByPage', res);
          if (res.code == 200) {
            this.tableData = res.data.records
          }
        })
      },
      handleAddImage() {
        this.resetForm()
        this.dialogFormVisible = true
      },
      handleEdit(row) {
        this.form = row
        this.form.updateTime = null
        this.dialogFormVisible = true
      },
      handleDelete(row) {
        console.log('handleDelete')
        if(confirm('删除后无法恢复，您确定？')){
          bannerDelBody({id:row.bodyId}).then(res=>{
            if(res.code==200){
              this.$message.success('删除成功')
              this.loadList()
            }
          })
        }
      },
      submitCarousel() {
        this.$refs['form'].validate((valid) => {
          if (valid) {
            let param = this.form
            if(this.form.bodyId == 0) {
              bannerAddBody(param).then(res => {
                if (res.code == 200) {
                  this.$message.success('添加成功')
                  this.dialogFormVisible = false
                  this.loadList()
                }
              })
            }
            else {
              bannerSetBody(param).then(res => {
                if (res.code == 200) {
                  this.$message.success('修改成功')
                  this.dialogFormVisible = false
                  this.loadList()
                }
              })
            }


          } else {
            this.$message.error('请把表单数据填写完整')
            return false;
          }
        });

      },
      handleCurrentChange(val) {
        console.log(`当前页: ${val}`)
        this.pageNum = val
        this.loadResourceImages()
      },
      handleSizeChange(val) {
        console.log(`每页 ${val} 条`)
        this.pageSize = val
        this.loadResourceImages()
      },
      handleShowImageForm() {
        this.loadResourceImages()
        this.imgFormVisible = true
      },
      loadResourceImages() {
        let param = {
          pageNum: this.pageNum,
          pageSize: this.pageSize,
          keyWord: 'image'
        }
        getResourceList(param).then(res => {
          console.log('getResourceList', res);
          if(res.code == 200) {
            this.dataImages = []
            res.data.records.forEach(item => {
              let img = {
                id: item.fileId,
                src: item.fileUrl,
                alt: item.fileName
              }
              this.dataImages.push(img)
            })
            // this.tableData = res.data.records
            this.pageNum = res.data.current
            this.pageSize = res.data.size
            this.total = res.data.total
          }
        })
      },
      onSelectImage(image) {
        console.log(image)
        this.form.imageUrl = image.src
      },
      submitSelectImage() {
        this.imgFormVisible = false
      },
    }
  }
</script>

<style>
  .box {
    margin: 10px;
  }
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409eff;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 160px;
    height: 90px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 160px;
    height: 90px;
    display: block;
  }
</style>
